package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-30 13:54
 */
public class Test27 {
    public static void main(String[] args) {

    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    private ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode res = new ListNode();
        ListNode p = res;
        // 1长 2短
        // 1 2 4 8 9 11
        // 2 3 5 6
        while (true) {
            if (list1 == null) {
                p.next = list2;
                break;
            }
            if (list2 == null) {
                p.next = list1;
                break;
            }
            if (list1.val <= list2.val) {
                ListNode temp = list1;
                list1 = list1.next;
                p.next = temp;
                temp.next = null;
                p = temp;
            } else {
                ListNode temp = list2;
                list2 = list2.next;
                p.next = temp;
                temp.next = null;
                p = temp;
            }
        }
        return res.next;
    }

}
